package com.消除ifelse.注解方式;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import java.util.HashMap;
import java.util.Map;

/**
 * @Date 2021/1/14 11:18
 * @Author WangLinBao
 * @Description
 */
public class PayService implements ApplicationListener<ContextRefreshedEvent> {
    private static Map<String,IPay> payMap = null;
    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        ApplicationContext applicationContext = contextRefreshedEvent.getApplicationContext();
        Map<String, Object> beans = applicationContext.getBeansWithAnnotation(PayCode.class);
        if(beans!=null){
            payMap = new HashMap<>();
            beans.forEach((key,value) ->{
                String type = value.getClass().getAnnotation(PayCode.class).value();
                payMap.put(type,(IPay) value);
            });
        }
    }
    public void pay(String code){
        payMap.get(code).pay();
    }
}
